package com.edgclub.edg.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;

public class MyInformationChangeNameActivity extends GzBaseTooolBarActivity implements View.OnClickListener , VolleyListener{
    private boolean isload=false;
    private EditText change_name;
    private UserPrefUtils userPrefUtils;
    private User user;
    @Override
    public int getContentView() {
        return R.layout.activity_change_name;
    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("修改昵称");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
        right_layout.setVisibility(View.VISIBLE);
        right_text.setText("保存");
        right_layout.setOnClickListener(this);
    }

    @Override
    public void initView() {
        change_name = (EditText) findViewById(R.id.change_name);
//        change_name.addTextChangedListener(textWatcher);
    }

    @Override
    public void initData() {
        userPrefUtils = new UserPrefUtils(this);
        user=userPrefUtils.getUser();
        if(null==user)user=new User();
        change_name.setText(user.getNick());
        change_name.setSelection(change_name.length());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_back:
                finish();
                break;
            case R.id.right_layout:
                String text=change_name.getText().toString();
                int num= StringUtils.getChatCount(text);
                int lengh=text.length();
                if(lengh+num>20){
                    showToast("昵称不能超过20字符");
                    return;
                }
                submit();
                break;

        }
    }

    private void submit() {
        if ("".equals(change_name.getText().toString().trim())){
            Toast.makeText(MyInformationChangeNameActivity.this,"请输入您要修改的名字",Toast.LENGTH_SHORT).show();
            return;
        }else {
            upName();
        }
    }
    private void upName(){
        if(isload)return;
        UserParam param = new UserParam();
        param.setUserId(userPrefUtils.getUser().getId());
        if("".equals(change_name.getText()+"")){
            StringUtils.postToast(this,"昵称不可为空！");
            return;
        }
        param.setNick(change_name.getText()+"");
        new UserApi(this).getupname(param,this);
    }



    @Override
    public void onStart(String tag) {
        isload=true;
        showDialog("请求中，请稍后...",false);
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        isload=false;
        if (JSONHelper.isSuccess(response)){
            user.setNick(change_name.getText().toString().trim());
            userPrefUtils.saveUser(user);
            Intent intent=new Intent(MyInformationChangeNameActivity.this,MyInforMationActivity.class);
            intent.putExtra("user", user);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        isload=false;
        dismissDialog();
        ResultCode.toastVolleyError(MyInformationChangeNameActivity.this, volleyError);
    }
//    private TextWatcher textWatcher = new TextWatcher() {
//        private int editStart;
//        private int editEnd;
//        private int maxLen = 20; // the max byte
//        @Override
//        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//            Log.d("TextChanged", "---->beforeTextChanged : start = " + start + " count = " + count + " after = " + after);
//        }
//
//        @Override
//        public void onTextChanged(CharSequence s, int start, int before, int count) {
//        }
//
//        @Override
//        public void afterTextChanged(Editable s) {
//            editStart = change_name.getSelectionStart();
//            editEnd = change_name.getSelectionEnd();
//            // 先去掉监听器，否则会出现栈溢出
//            change_name.removeTextChangedListener(textWatcher);
//            if (!TextUtils.isEmpty(change_name.getText())) {
//                String etstring = change_name.getText().toString().trim();
//                while (calculateLength(s.toString()) > maxLen) {
//                    s.delete(editStart - 1, editEnd);
//                    editStart--;
//                    editEnd--;
//                    Log.d("TextChanged", "editStart = " + editStart + " editEnd = " + editEnd);
//                }
//            }
//
//            change_name.setText(s);
//            change_name.setSelection(editStart);
//
//            // 恢复监听器
//            change_name.addTextChangedListener(textWatcher);
//            // end by zyf --------------------------
//        }
//        private int calculateLength(String etstring) {
//            char[] ch = etstring.toCharArray();
//
//            int varlength = 0;
//            for (int i = 0; i < ch.length; i++) {
//                // changed by zyf 0825 , bug 6918，加入中文标点范围 ， TODO 标点范围有待具体化
//                if ((ch[i] >= 0x2E80 && ch[i] <= 0xFE4F) || (ch[i] >= 0xA13F && ch[i] <= 0xAA40) || ch[i] >= 0x80) { // 中文字符范围0x4e00 0x9fbb
//                    varlength = varlength + 2;
//                } else {
//                    varlength++;
//                }
//            }
//            Log.d("TextChanged", "varlength = " + varlength);
//            // 这里也可以使用getBytes,更准确嘛
//            // varlength = etstring.getBytes(CharSet.forName("GBK")).lenght;// 编码根据自己的需求，注意u8中文占3个字节...
//            return varlength;
//        }
//    };
}
